package com.ftg.learn.bus.service.impl;

import com.ftg.learn.bus.entity.Approval;
import com.ftg.learn.bus.entity.LoginUser;
import com.ftg.learn.bus.lnterceptor.LoginInterceptor;
import com.ftg.learn.bus.mapper.ApprovalMapper;
import com.ftg.learn.bus.service.IApprovalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 审批表 服务实现类
 * </p>
 *
 * @author lalala
 * @since 2022-03-07
 */
@Service
public class ApprovalServiceImpl extends ServiceImpl<ApprovalMapper, Approval> implements IApprovalService {

    @Resource
    ApprovalMapper approvalMapper;

    /**
     * 修改审核表内信息，并根据通过状态同步到经费支出表的审核状态
     *
     * @param beid
     * @param is_passed
     * @return
     */
    @Transactional(rollbackFor = SQLException.class)
    @Override
    public boolean updateApp(Long aid, Long beid, int is_passed, String remarks) {
        LoginUser loginUser = LoginInterceptor.loginUserThreadLocal.get();
        Long uaid = loginUser.getId();
        Map<String, Object> map = approvalMapper.selectAnameAphone(uaid);
        Approval approval = new Approval();
        approval.setAid(aid);
        approval.setBeid(beid);
        approval.setUaid(uaid);
        approval.setAname(String.valueOf(map.get("uname")));
        approval.setAphone(String.valueOf(map.get("uphone")));
        approval.setIspassed(is_passed);
        approval.setRemarks(remarks);
        //  修改时间也要更改
        approval.setApprovalTime(LocalDateTime.now());
        this.updateById(approval);
        int ex_status = 0;
        if (approval.getIspassed() == 2) {
            ex_status = 2;
        }
        approvalMapper.updateEx(ex_status, LocalDateTime.now(),approval.getBeid());
        return true;
    }
}
